<?php 
if(!$_SESSION['admin'] || !$index)
{
    include('404.php');
}
else
{
?>
    <script type="text/javascript">
        function checkboxDisable() {
            var mycheckbox = document.getElementById('delcheckbox').checked;
            if(mycheckbox)
            {
                document.getElementById('deletejabutton').disabled = false;
            }
            else
            {
                document.getElementById('deletejabutton').disabled = true;
            }
        }
    </script>
<?php
if(isset($_POST['newgroupsubmit']))
{
    if(strlen($_POST['newgroupname']) >= 4)
    {
        $ps = new PreparedStatement("INSERT INTO groepen (groepnaam, startjaar, eindjaar) VALUES (?, ?, ?);");
        $ps->setString($_POST['newgroupname']);
        $ps->setString($_POST['newgroupstart']);
        $ps->setString($_POST['newgroupend']);
        $ps->execute();
        if(!$ps->get_id())
        {
            die("MySQL error");
        }
    }
    else
    {
        $newgroupnamefail = true;
    }
}
else if(isset($_POST['delja']))
{
    $ps = new PreparedStatement("DELETE FROM groepen WHERE groepid = ?;");
    $ps->setInt($_POST['deletegroupvalue']);
    $ps->execute();
}
else if(isset($_GET['action']))
    if($_GET['action'] == 'delete')
    {
        echo "<br /><center>Weet u zeker dat u deze groep wil verwijderen?<br /><br />";
        echo "<input onclick='checkboxDisable(this)' id='delcheckbox' type='checkbox' /> Ja ik weet het zeker.<br />";
        echo "<form name='deletef1' action='?p=groepenbeheren' style='display:inline;' method='post'><input name='delja' type='hidden' value='delja' /><input type='hidden' name='deletegroupvalue' value='{$_GET['value']}' /><input id='deletejabutton' disabled='disabled' type='submit' value='Ja' /></form>";
        echo "<form name='deletef2' action='?p=groepenbeheren' style='display:inline;' method='post'><input type='hidden' value='delnee' /><input type='submit' value='Nee' /></form></center>";
    }
    
    
if(!isset($_GET['action']))
{
?>
<center>
<h2>Groepen Beheren</h2>
<link rel="stylesheet" type="text/css" href="styles/inschrijvingen.css" />
<?php
$ps = new PreparedStatement("SELECT * FROM groepen ORDER BY eindjaar ASC;");
$q = $ps->execute();
echo "<table>";
echo "<tr><td style='width: 200px' class='toprow'>Groepnaam: </td><td style='width: 80px; text-align:center;' class='toprow'>Startjaar: </td><td style='width: 80px; text-align:center;' class='toprow'>Eindjaar: </td><td class='toprow'>Acties:</td></tr>";
while($row = mysql_fetch_array($q))
{
    $color = 'black';
    if($row['eindjaar'] < date('Y'))
    {
        $color = 'red';
    }
    else if($row['eindjaar'] == date('Y'))
    {
        $color = 'blue';
    }
    
    echo "<tr>
              <td style='color: $color;'>{$row['groepnaam']}</td>
              <td style='color: $color; text-align:center;'>{$row['startjaar']}</td>
              <td style='color: $color; text-align:center;'>{$row['eindjaar']}</td>
              <td>
                  <center>
                      <a href='?p=groepaanpassen&groepid={$row['groepid']}'><img title='Aanpassen' src='images/icons/settings.gif' /></a>
                      <a href='?p=groepenbeheren&action=delete&value={$row['groepid']}'><img title='Verwijderen' src='images/icons/minus.gif' /></a>
                  </center>
              </td>
         </tr>";
}
?>
</table>
<br /><br /><br />
<div style='padding: 10px; width: 340px; border: solid; border-width: 1px;'>
    <b>Groep Toevoegen</b>
    <br />
    <form method="post">
    <table>
        <tr>
            <td style='width: 100px;'>Groepnaam:</td>
            <td><input type='text' name='newgroupname' /></td>
        </tr>
        <tr>
            <td colspan="2"><?php if(isset($newgroupnamefail)) echo "<font color='red'>Groepnaam moet langer zijn dan 3 tekens.</font>" ?></td>
        </tr>
        <tr>
            <td style='width: 100px;'>Periode:</td>
            <td>
                <select name='newgroupstart'>
                    <?php
                        for($i = date('Y'); $i < date('Y') + 5; $i++)
                        {
                            echo "<option value='$i'>$i</option>";
                        }
                    ?>
                </select>
                 - 
               <select name='newgroupend'>
                    <?php
                        for($i = date('Y') + 3; $i < date('Y') + 8; $i++)
                        {
                            echo "<option value='$i'>$i</option>";
                        }
                    ?>
                </select>
            </td>
        </tr>
        <tr>
            <td style='width: 100px;'></td>
            <td><input type='submit' value='Groep Toevoegen' name='newgroupsubmit' /></td>
        </tr>
    </table>
    </form>
</div>

</center>
<?php
}
}
?>